﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AutoFillCenter
{
    public class YahooAutoFill : AutoFill
    {
        readonly int _id = 2;
        readonly String _name = "Yahoo AutoFill";

        public override bool processFill()
        {
            Int32 main = DllLoader.FindWindow("YahooBuddyMain", null);
            programID = main;
            if (main == 0)
                return false;
            Int32 parent = DllLoader.FindWindowEx(main,0,null, null);
            if (parent == 0)
                return false;
            usernameField = DllLoader.FindWindowEx(parent, 0 , "Edit", null);
            passwordField = DllLoader.FindWindowEx(parent, usernameField, "Edit", null);
            if (usernameField == 0 || passwordField == 0)
            {
                return false;
            }
            submitField = DllLoader.FindWindowEx(parent, passwordField, "Button", "&Sign In");
            if (submitField == 0)
                return false;
            
            return true;
        }

        public override bool Fill(String username, String password)
        {
            if(usernameField==0){
                if(!processFill())
                    return false;
            }
            DllLoader.SendMessage(usernameField, DllLoader.WM_SETTEXT, 0, username);
            DllLoader.SendMessage(passwordField, DllLoader.WM_SETTEXT, 0, password);
            DllLoader.SetForegroundWindow(programID);
            if (IS_AUTO_SUBMIT)
            {
                Submit();
            }
            
            return true;
        }

        public override bool Submit()
        {
            if(submitField==0)
                if(!processFill())
                    return false;
            DllLoader.SendMessage(submitField, DllLoader.BM_CLICK, 0, 0);
            return true;
        }
    }
}
